package com.example.anyong17_julnumgi;

import java.util.ArrayList;





import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;



import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class SensorSetActivity extends Activity {
        SensorManager mSm;
        TextView mCounterText;
        
        int mCounter = 0;//줄넘기 카운터
        int sufNum = 0;//추가 설정값
        int tempsufNum= 0;
        int sumCount= 0;
        long mApplyTime ;
        int mSenGap= 1000;
        float mSenRange = 16.0f;
        int sSenSpeed = 800;
        
        ArrayList<AccelValue> arValue = new ArrayList<AccelValue>();
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.sensorset);
        
                SharedPreferences pref = getSharedPreferences("JUL", 0);
                sufNum = pref.getInt("sufNum", 0);
                
                mSm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
                mSm.registerListener(mSensorListenr, mSm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_GAME);
                
                mCounterText= (TextView) findViewById(R.id.sensorest_counter);
                mCounterText.setText(""+sufNum);
                
                tempsufNum=sufNum;
        
                
        }
        @Override
        protected void onDestroy() {
                mSm.unregisterListener(mSensorListenr);
                RecycleUtils.recursiveRecycle(getWindow().getDecorView());
                System.gc();
                super.onDestroy();
        }
        protected void onPause() {
                super.onPause();
                SharedPreferences pref = getSharedPreferences("JUL", 0);
                SharedPreferences.Editor edit = pref.edit();
                
                edit.putInt("sufNum", sufNum);
                edit.commit();
        };//anyong
        SensorEventListener mSensorListenr = new SensorEventListener() {
                
                

                @Override
                public void onAccuracyChanged(Sensor sensor, int accuracy) {}
                
                @Override
                public void onSensorChanged(SensorEvent event) {
                        switch (event.sensor.getType()){
                                 case Sensor.TYPE_ACCELEROMETER:
                                         AccelValue av2 =new AccelValue();
                                         av2.value[0]= event.values[0];
                                         av2.value[1]= event.values[1];
                                         av2.value[2]= event.values[2];
                                         av2.time = event.timestamp/10000L;
                                         
                                         arValue.add(av2);
                                         
                                         long now = System.currentTimeMillis();
                                         if(now - mApplyTime<mSenGap){
                                                 break;
                                         }
                                         
                                         float[]min = new float[]{100,100,100};
                                         float[]max = new float[]{-100,-100,-100};
                                         for(int i= arValue.size()-1;i>=0;i--){
                                                 AccelValue v = arValue.get(i);
                                                 for(int j = 0;j<3;j++){
                                                         min[j] = Math.min(min[j],v.value[j]);
                                                         max[j] = Math.max(max[j],v.value[j]);
                                                 }
                                                 
                                         }
                                         float[] diff = new float[3];
                                         for(int j=0;j<3;j++){
                                                 diff[j] = Math.abs(max[j]-min[j]);
                                        }
                        
                                        if(diff[0]>mSenRange){
                                                mApplyTime =now;
                                                mCounter++;
                                                sumCount=sufNum+mCounter; 
                                                mCounterText.setText(""+sumCount);
                                                arValue.clear();
                                                
                                        }
                        }
                }
        };
        public void mOnClick(View v){
                switch(v.getId()){
                        
                        case R.id.sensorest_clear_btn:
                                sumCount = 0;
                                sufNum=0;
                                tempsufNum=0;
                                mCounterText.setText("0");
                                //Toast.makeText(this, "초기화", Toast.LENGTH_LONG).show();
                                break;
                        case R.id.sensorest_left_btn:
                                //tempsufNum=sufNum;
                                tempsufNum--;
                                sumCount=mCounter+tempsufNum;
                                mCounterText.setText(""+sumCount);
                                //Toast.makeText(this, "설정값="+sumCount, Toast.LENGTH_LONG).show();
                                break;
                        case R.id.sensorest_right_btn:
                                tempsufNum++;
                                sumCount=mCounter+tempsufNum;
                                mCounterText.setText(""+sumCount);
                                //Toast.makeText(this, "설정값="+sumCount, Toast.LENGTH_LONG).show();
                                break;
                        case R.id.sensorest_save_btn:
                                sufNum=tempsufNum;
                                Toast.makeText(this, /*"설정값="+sufNum+*/"저장되었습니다.", Toast.LENGTH_SHORT).show();
                                break; 
                }
        }
        
        
}